import { createRouter, createWebHashHistory } from 'vue-router'
import readingNotes from './readingNotes/index'
import workSummary from './workSummary/index'
import lifeInsights from './lifeInsights/index'
import collectLinks from './collectLinks/index'
import tools from './tools/index'
import other from './other/index'
import { useLoadingStore } from '@/stores/loading'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/resume',
            name: 'resume',
            component: () => import('@/pages/resume/Main.vue')
        },
        ...other,
        ...tools,
        {
            path: '/layout',
            name: 'layout',
            component: () => import('@/layout/Main.vue'),
            children: [
                { path: '/home', name: 'home', component: () => import('@/pages/home/Main.vue') },
                { path: '/tools', name: 'tools', component: () => import('@/pages/tools/Main.vue') },
                { path: '/nightReading', name: 'nightReading', component: () => import('@/pages/nightReading/Main.vue') },
                ...readingNotes,
                ...workSummary,
                ...lifeInsights,
                ...collectLinks,
                { path: '/', redirect: 'home' }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    const loadingStore = useLoadingStore()
    if (to.path !== from.path) {
        loadingStore.isShow = true
    }
    next(true)
})

router.afterEach(() => {
    const loadingStore = useLoadingStore()
    setTimeout(() => {
        loadingStore.isShow = false
    }, 800)
})

export default router
